<?php
MYTCONN();
echo '<div id="PBC_Pool"></div>';
$amlChannel = ' ';
$AMLNumber = '';
$AMLDate = '';
$AMLStatus = '';
$AMLOffice = '';
$AmlLevel = '';
if(isset($_POST['aml_Channel']) && $_POST['aml_Channel']>0){
$amlChannel = ' AND amh_Channel = '.$_POST['aml_Channel'];
}
if (isset($_POST['VALUE']) && $_POST['VALUE'] != '') {
    $AMLNumber = " amh_ID = {$_POST['VALUE']}";
}
if (isset($_POST['Level']) && $_POST['Level'] > 0) {
    $AmlLevel = " AND amh_levelid IN (6,7,9,10,11)";
}
if (isset($_POST['Status']) && $_POST['Status'] != 0) {
    $AMLStatus = " AND amh_Status = {$_POST['Status']}";
}
if ($_POST['TStart'] == '') {
    $_POST['TStart'] = date('Y-m-d H:i:s');
}
if ($_POST['TEnd'] == '') {
    $_POST['TEnd'] = date('Y-m-d H:i:s');
}
if ($_POST['Office'] != '') {
    $AMLOffice = " AND FraOrigen0 = {$_POST['Office']}";
}

if ($AMLNumber == '' && $AMLDate == '' && $AMLStatus == '') {
    $AMLDate = "amh_Fecha = '" . date("Y-m-d") . "'";
}
if ($_POST['CLIENT'] != '') {
    $QrCond = "amh_CLIENT = {$_POST['CLIENT']}";
} elseif ($_POST['VALUE'] != '') {
    $QrCond = $AMLNumber;
} else {
    $QrCond = "((DATE(amh_Fecha) BETWEEN '{$_POST['TStart']}' AND '{$_POST['TEnd']}'{$AMLOffice})
		OR (DATE(amh_lastevent) BETWEEN '{$_POST['TStart']}' AND '{$_POST['TEnd']}'{$AMLOffice}))
   {$AMLStatus}{$AmlLevel}";
}

$GET = $MYT->db_array("SELECT
  				amh_ID AS O_ID,
				amh_Operacion AS COL1,
				amh_Fecha AS COL2,
				stt_Word AS COL3,
				'' AS COL4,
				name AS COL5,
				amh_FechaComenta AS COL6,
				CONCAT(FraOrigen0,'-',LPAD(FraReferenciaAg,6,0)) AS COL7,
				amh_CLIENT AS COL8
  FROM hd_amlalertas
  INNER JOIN hd_amldetalle ON amd_Alerta = amh_ID AND amd_Code = {$_POST['Tipo']}
  INNER JOIN facturas ON amh_Operacion = FraID
  LEFT OUTER JOIN systemoffice.main_users ON ref = FraOrigen2
  INNER JOIN hd_sysstatus ON hd_sysstatus.sttFlag = amh_Status AND stt_Sistema = 14
	WHERE $QrCond $amlChannel");
$CARD = $GET[0];
?>

<script charset="utf-8" type="text/javascript">
    disableSelection(document.getElementById("PBC_Pool"));

    Ext.onReady(function(){

        var myData = [
<?php
for ($i = 1; $i <= $CARD; $i++) {
    echo '[' . $i . ',' . $GET[$i]['O_ID'] . ',\'' . $GET[$i]['COL1'] . '\',\'' . $GET[$i]['COL2'] . '\',\'' . $GET[$i]['COL3'] . '\',\'' . $GET[$i]['COL4'] . '\',\'' . $GET[$i]['COL5'] . '\',\'' . $GET[$i]['COL6'] . '\',\'' . $GET[$i]['COL7'] . '\',' . $GET[$i]['COL8'] . ']';
    if ($i < $CARD) {
        echo ',';
    }
}
?>
            ];

            var ds = new Ext.data.Store({
                reader: new Ext.data.ArrayReader({}, [
                    {name: 'id'},
                    {name: 'O_ID', type: 'int'},
                    {name: 'COL1', type: 'string'},
                    {name: 'COL2', type: 'string'},
                    {name: 'COL3', type: 'string'},
                    {name: 'COL4', type: 'string'},
                    {name: 'COL5', type: 'string'},
                    {name: 'COL6', type: 'string'},
                    {name: 'COL7', type: 'string'},
                    {name: 'COL8', type: 'string'}
                ])
            });
            ds.loadData(myData);
            var colModel = new Ext.grid.ColumnModel([
                {header: "N", width: 40, sortable: true, locked:true, dataIndex: 'id'},
                {header: "Alerta", width: 60, sortable: true, locked:true, dataIndex: 'O_ID'},
                {header: "Cliente", width: 80, sortable: true, locked:true, dataIndex: 'COL8'},
                {header: "Operação", width: 70, sortable: true, locked:true, dataIndex: 'COL1'},
                {header: "Referencia", width: 90, sortable: true, locked:true, dataIndex: 'COL7'},
                {header: "Data", width: 120, sortable: true, dataIndex: 'COL2'},
                {header: "Estado", width: 70, sortable: true, locked:true,dataIndex: 'COL3'},
                {header: "Funcionario", width: 100, sortable: true, locked:true,dataIndex: 'COL5'},
                {header: "Data Resolução", width: 120, sortable: true, align:'right', locked:true,dataIndex: 'COL6'},
                {header: "Comentario", width: 200, locked:true, dataIndex: 'COL4'}
            ]);

            var selmode = new Ext.grid.RowSelectionModel({});
            var Console = new Ext.grid.GridPanel({
                layout: 'fit',
                renderTo: 'PBC_Pool',
                height: 180,
                ds: ds,
                cm: colModel,
                sm: selmode,
                listeners: {
                    rowclick: function(ds, row) {
                        var record = ds.getStore().getAt(row);
                        var value = record.get('O_ID');
                        replaceUrl('element','_response/call.php?get=Compliance/aml_detal&AMLID='+value);
                    }
                }
            });
        });
</script>